package com.zjitc.controller;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageInfo;
import com.zjitc.po.Country;
import com.zjitc.service.CountryService;
import java.util.Arrays;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

/**
 * Create by IntelliJ IDEA
 *
 * @author: jsonor
 * @date-Time: 2018/3/26 15:51
 * @description:
 */
@Controller
public class CountryController {

  @Autowired
  private CountryService countryService;
  @RequestMapping("findAll")
  public void findCouties(){
    List<Country> countries=countryService.findAllCountries();
    System.out.println(Arrays.toString(countries.toArray()));
    StringBuilder builder=new StringBuilder();
    for (Country country:countries){
      builder.append(country.toString());
      builder.append("\n");
    }
    System.out.println(builder);
  }

  @RequestMapping("findPage")
  public ModelAndView findPageCountries(
      @RequestParam(name = "start",required = false,defaultValue = "1") int  start,
      @RequestParam(name = "count",required = false,defaultValue = "10")int count){
    List<Country> countries=countryService.findCountries(start,count);
    StringBuilder builder=new StringBuilder();
    for (Country country:countries){
      builder.append(country.toString());
      builder.append("\n");
    }
    System.out.println(builder);

    System.out.println((Page)countries);
    PageInfo info=new PageInfo();
    System.out.println(info);

    ModelAndView modelAndView= new ModelAndView();
    modelAndView.addObject("country",countries);
    modelAndView.setViewName("/view/country.jsp");
    return modelAndView;
  }
}
